package net.moao.netutils.customView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import net.moao.netutils.HttpActivity;
import net.moao.netutils.R;
import net.moao.netutils.utils.AppConfig;
import net.moao.netutils.utils.MapUtils;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class HeadersEditDialogFragment extends DialogFragment {
    private Activity CreateActivity;
    private EditText editKey, editValue;
    private Button btnAdd, btnCancel, btnSave;
    private ListView listView;
    private HeadersMapAdapter adapter;
    private Map<String, String> map = new HashMap<>();
    private Set<String> KeySetString = null;
    private Set<String> ValueSetString = null;

    public HeadersEditDialogFragment(Activity activity){
        CreateActivity = activity;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_headers_edit, null);

        editKey = view.findViewById(R.id.edit_key);
        editValue = view.findViewById(R.id.edit_value);
        btnAdd = view.findViewById(R.id.btn_add);
        btnCancel = view.findViewById(R.id.btn_cancel);
        btnSave = view.findViewById(R.id.btn_save);
        listView = view.findViewById(R.id.list_map);

        editKey.setText("Authorization");

        // 加载本地 Headers 数据
        KeySetString = AppConfig.getStringSet(CreateActivity, HttpActivity.LocalStorageField.REQUEST_HEADERS_KEY.getFieldName());
        ValueSetString = AppConfig.getStringSet(CreateActivity, HttpActivity.LocalStorageField.REQUEST_HEADERS_VALUE.getFieldName());
        if (KeySetString != null && ValueSetString != null) {
            map = MapUtils.createMapFromSets(KeySetString, ValueSetString);
        }

        List<Map.Entry<String, String>> entries = new ArrayList<>(map.entrySet());
//        adapter = new HeadersMapAdapter(getActivity(), android.R.layout.simple_list_item_2, entries);
        adapter = new HeadersMapAdapter(getActivity(), R.layout.list_item_header, entries);
        listView.setAdapter(adapter);

        btnAdd.setOnClickListener(v -> {
            String key = editKey.getText().toString();
            String value = editValue.getText().toString();
            if (!key.isEmpty() && !value.isEmpty()) {
                map.put(key, value);
                entries.add(new AbstractMap.SimpleEntry<>(key, value));
                adapter.notifyDataSetChanged();
                editKey.setText("Authorization");
                editValue.setText("");
            }
        });

        listView.setOnItemLongClickListener((parent, view1, position, id) -> {
            Map.Entry<String, String> entry = entries.get(position);
            map.remove(entry.getKey());
            entries.remove(position);
            adapter.notifyDataSetChanged();
            return true;
        });

        btnCancel.setOnClickListener(v -> dismiss());
        btnSave.setOnClickListener(v -> {
            // Do something with the updated map
            AppConfig.putStringSet(CreateActivity, HttpActivity.LocalStorageField.REQUEST_HEADERS_KEY.getFieldName(), MapUtils.getKeysFromMap(map));
            AppConfig.putStringSet(CreateActivity, HttpActivity.LocalStorageField.REQUEST_HEADERS_VALUE.getFieldName(), MapUtils.getValuesFromMap(map));
            dismiss();
            ((TextView)CreateActivity.findViewById(R.id.msg)).append("保存成功 Headers: " + map + "\n");
        });

        builder.setView(view);
        return builder.create();
    }
}